﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Logging;
using System.Data;
using System.Text;
using VAdvantage.Controls;
using VAdvantage.Model;
using VAdvantage.DataBase;
using SL4PopupMenu;
using VAdvantage.Utility;
using VAdvantage.Apps;
using VAdvantage.Apps.AForms;

namespace VAdvantage.Framework
{
    public class AArchive
    {
        public AArchive(Object invoker, int AD_Table_ID, int Record_ID)
        {
            log.Config("AD_Table_ID=" + AD_Table_ID + ", Record_ID=" + Record_ID);
            m_AD_Table_ID = AD_Table_ID;
            m_Record_ID = Record_ID;
        }

        /**	The Table						*/
        private int m_AD_Table_ID;
        /** The Record						*/
        private int m_Record_ID;
        /**	Logger	*/
        private static VLogger log = VLogger.GetVLogger(typeof(AArchive).FullName);

        PopupMenuItem m_documents = null;
        PopupMenuItem m_reports = null;
        PopupMenuItem m_reportsAll = null;

        StringBuilder m_where = null;

        //private void GetArchives(ToolBarButton invoker)
        //{
        //    int reportCount = 0;
        //    int documentCount = 0;

        //    m_where = new StringBuilder();
        //    m_where.Append("(AD_Table_ID=").Append(m_AD_Table_ID)
        //        .Append(" AND Record_ID=").Append(m_Record_ID)
        //        .Append(")");
        //    //	Get all for BP
        //    if (m_AD_Table_ID == X_C_BPartner.Table_ID)
        //        m_where.Append(" OR C_BPartner_ID=").Append(m_Record_ID);
        //    //
        //    StringBuilder sql = new StringBuilder("SELECT IsReport, COUNT(*) FROM AD_Archive ")
        //        .Append("WHERE (AD_Table_ID=" + m_AD_Table_ID + " AND Record_ID=" + m_Record_ID + ") ");
        //    if (m_AD_Table_ID == X_C_BPartner.Table_ID)
        //        sql.Append(" OR C_BPartner_ID=" + m_Record_ID);
        //    sql.Append(" GROUP BY IsReport");

        //    IDataReader dr = null;
        //    try
        //    {
        //        dr = DB.ExecuteReader(sql.ToString(), null);
        //        while (dr.Read())
        //        {
        //            if ("Y".Equals(dr[0].ToString()))
        //                reportCount += int.Parse(dr[1].ToString());
        //            else
        //                documentCount += int.Parse(dr[1].ToString());
        //        }
        //        dr.Close();
        //    }
        //    catch (Exception e)
        //    {
        //        dr.Close();
        //        log.Log(Level.SEVERE, sql.ToString(), e);
        //    }

        //    //
        //    if (documentCount > 0)
        //    {
        //        m_documents = new PopupMenuItem(null, Msg.GetMsg(Envs.GetCtx(), "ArchivedDocuments" + " (" + documentCount + ")"));
        //        //invoker.DropDownItems.Add(m_documents);
        //        m_documents.Click += new RoutedEventHandler(m_reportsAll_Click);
        //    }
        //    if (reportCount > 0)
        //    {
        //        m_reports = new PopupMenuItem(null, Msg.GetMsg(Envs.GetCtx(), "ArchivedReports")
        //            + " (" + reportCount + ")");
        //        //invoker.DropDownItems.Add(m_reports);
        //        m_reports.Click += new RoutedEventHandler(m_reportsAll_Click);
        //    }
        //    //	All Reports
        //    String sql1 = "SELECT COUNT(*) FROM AD_Archive WHERE AD_Table_ID=@param1 AND IsReport='Y'";
        //    int allReports = DB.GetSQLValue(null, sql1, m_AD_Table_ID);
        //    if (allReports > 0)
        //    {
        //        m_reportsAll = new PopupMenuItem(null, Msg.GetMsg(Envs.GetCtx(), "ArchivedReportsAll")
        //            + " (" + reportCount + ")");
        //        //invoker.DropDownItems.Add(m_reportsAll);
        //        m_reportsAll.Click += new RoutedEventHandler(m_reportsAll_Click);
        //    }

        //    if (documentCount == 0 && reportCount == 0 && allReports == 0)
        //    {
        //        //invoker.DropDownItems.Add(Msg.GetMsg(Env.GetCtx(), "ArchivedNone"));
        //    }
        //    //
        //    //invoker.ShowDropDown();	//	below button
        //}

        void m_reportsAll_Click(object sender, RoutedEventArgs e)
        {
            int AD_Form_ID = 118;	//	ArchiveViewer
            FormFrame ff = new FormFrame();
            ff.OpenForm(AD_Form_ID);
            ArchiveViewer av = (ArchiveViewer)ff.GetFormPanel();
            //
            if (sender == m_documents)
                av.Query(false, m_AD_Table_ID, m_Record_ID);
            else if (sender == m_reports)
                av.Query(true, m_AD_Table_ID, m_Record_ID);
            else	//	all Reports
                av.Query(true, m_AD_Table_ID, 0);
            //
            ff.SetMaximize(false);
            //ff.Dock = DockStyle.Fill;
            ff.Show();
            ff = null;
        }


        public void OpenArchiveForm()
        {
            int AD_Form_ID = 118;	//	ArchiveViewer
            FormFrame ff = new FormFrame();
            ff.SetTable_ID(m_AD_Table_ID);
            ff.OpenForm(AD_Form_ID);
            //ArchiveViewer av = (ArchiveViewer)ff.GetFormPanel();
            //av.Query(true, m_AD_Table_ID, 0);
            //ff.SetMaximize(true);
            //ff.Show();
        }

    }
}
